package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ConfirmEditServlet extends HttpServlet {
    private static final String MAIN = "/WEB-INF/jsp/catalog/main.jsp";
    private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/editAccountForm.jsp";

    private Account account;

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String password = req.getParameter("password");
        String repeatedPassword = req.getParameter("repeatedPassword");
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address1 = req.getParameter("address1");
        String address2 = req.getParameter("address2");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zip = req.getParameter("zip");
        String country = req.getParameter("country");
        String languages = req.getParameter("languagePreference");
        String categories = req.getParameter("favouriteCategoryId");

        boolean listOption;
        if(req.getParameter("listOption") != null){
            listOption =  true;
        }else{
            listOption = false;
        }

        boolean bannerOption;
        if(req.getParameter("bannerOption") != null){
            bannerOption =  true;
        }else{
            bannerOption = false;
        }

        if(password.equals(repeatedPassword)){
            HttpSession session = req.getSession();
            account = (Account)session.getAttribute("loginAccount");
            account.setPassword(password);
            account.setFirstName(firstName);
            account.setLastName(lastName);
            account.setEmail(email);
            account.setPhone(phone);
            account.setAddress1(address1);
            account.setAddress2(address2);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            account.setLanguagePreference(languages);
            account.setFavouriteCategoryId(categories);
            account.setListOption(listOption);
            account.setBannerOption(bannerOption);

            AccountService accountService = new AccountService();
            accountService.updateAccount(account);
            // 覆盖原来的account
            session.setAttribute("account",account);

            Account account = (Account) session.getAttribute("account");
            if(account != null) {
                String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + "  "+"编辑账户信息成功转至主页面";
                logService.insertLogInfo(account.getUsername(),logInfo);
            }

            req.getRequestDispatcher(MAIN).forward(req,resp);
        }else{
            String message = "password error";
            req.setAttribute("message",message);
            req.getRequestDispatcher(EDIT_ACCOUNT).forward(req,resp);
        }
    }
}
